/* Name: John Gideon
 * Date: 4-3-12
 * Description: Draws the GUI, accepts button presses and new event data
 * 
 * Revisions:
 * -4/3/12 - John Gideon - Original
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

// Gui class draws a GUI for interaction with the user.
public class Gui {
	private JFrame jfrm;
	private JTextField[] outField;
	private JTextField infoField;
	private JTextField priorityField;
	private EventExecutor eventExecutor;
	private JButton nextBtn;
	private JButton newBtn;
	private JButton stopBtn;
	
	// Function called when the next button is pressed
	ActionListener nextListener = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			if (eventExecutor.nextEvent() == 0) {
				nextBtn.setEnabled(false);
				newBtn.setEnabled(false);
				stopBtn.setEnabled(false);
			}
			else{
				newBtn.setEnabled(true);
			}
		}
	};
	
	// Function called when the new button is pressed
	ActionListener newListener = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			if (eventExecutor.newEvent(priorityField.getText(),infoField.getText())) {
				newBtn.setEnabled(false);
			}
		}
	};
	
	// Function called when the stop button is pressed
	ActionListener stopListener = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			eventExecutor.stopEvent();
			nextBtn.setEnabled(false);
			newBtn.setEnabled(false);
			stopBtn.setEnabled(false);
		}
	};
	
	// Create the GUI frame
	public Gui() {	
		// Create Frame
		jfrm=new JFrame("Event Executor");
		jfrm.setSize(400,500);
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setLayout(null);
		jfrm.setResizable(false);
		
		// Create Output Boxes
		JLabel outLab=new JLabel("Previously Executed Events:");
		outLab.setBounds(10, 10, 375, 30);
		jfrm.add(outLab);
		outField = new JTextField[10];
		for (int i = 0; i < 10; i++) {
			outField[i] = new JTextField();
			outField[i].setBounds(10, 40+(30*i), 375, 30);
			outField[i].setEditable(false);
			jfrm.add(outField[i]);
		}
		
		// Create Next Button
		nextBtn = new JButton("Next");
		nextBtn.setBounds(10, 350, 120, 30);
		nextBtn.addActionListener(nextListener);
		jfrm.add(nextBtn);
		
		// Create New Button
		newBtn = new JButton("New");
		newBtn.setBounds(135, 350, 120, 30);
		newBtn.addActionListener(newListener);
		jfrm.add(newBtn);
		newBtn.setEnabled(false);
		
		// Create Stop Button
		stopBtn = new JButton("Stop");
		stopBtn.setBounds(260, 350, 120, 30);
		stopBtn.addActionListener(stopListener);
		jfrm.add(stopBtn);
		
		// Create Information Input Box
		JLabel infoLab=new JLabel("New Event INF:");
		infoLab.setBounds(15, 390, 110, 30);
		jfrm.add(infoLab);
		infoField = new JTextField();
		infoField.setBounds(130, 390, 200, 30);
		jfrm.add(infoField);
		
		// Create Priority Input Box
		JLabel priorityLab=new JLabel("New Event PR:");
		priorityLab.setBounds(15, 430, 110, 30);
		jfrm.add(priorityLab);	
		priorityField = new JTextField();
		priorityField.setBounds(130, 430, 200, 30);
		jfrm.add(priorityField);
		
		jfrm.setVisible(true);
	}
	
	public void setEventExecutor(EventExecutor uEventExecutor) {
		eventExecutor = uEventExecutor;
	}
	
	// Add a new line of text to the screen and shift previous lines upward
	public void pushText(String text) {
		for (int i = 0; i < 9; i++) {
			outField[i].setText(outField[i+1].getText());
		}
		outField[9].setText(text);
	}
	
}
